package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.VerificacionService;
import co.gov.medellin.ssti.sub.model.vo.VerificacionVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;
import co.gov.medellin.ssti.sub.web.form.StrutsUploadForm;

public class IngresoArchivoVerificacionAction extends Action {
	public static final String BENEFICIARIO = "BENEFICIARIO";
	public static final String SISBEN = "SISBEN";
	public static final String BENBOT = "BENBOT";

	@Autowired
	VerificacionService verificacionServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		HttpSession session = request.getSession();
		StrutsUploadForm formBean = (StrutsUploadForm) form;
		UserVO user = (UserVO) session.getAttribute("userInfo");
		String tipo = (String) request.getAttribute("tipo");
		if (tipo == null || tipo.isEmpty()) {
			tipo = request.getParameter("tipo");
		}
		formBean.setTipo(tipo);
		ArrayList<VerificacionVO> verificados = new ArrayList<VerificacionVO>();

		try {
			if (tipo != null && !tipo.isEmpty()) {

				if (tipo.equalsIgnoreCase(BENBOT)) {
					verificados = (ArrayList<VerificacionVO>) verificacionServiceImpl
							.findVerificationBeneficiarioBeneficio(user
									.getName());
					if (!verificados.isEmpty()) {
						formBean.setVerificados(verificados);
						request.setAttribute("verificados", verificados);
					}
				} else if (tipo.equalsIgnoreCase(SISBEN)) {
					verificados = (ArrayList<VerificacionVO>) verificacionServiceImpl
							.findVerificationSisben(user.getName());
					if (!verificados.isEmpty()) {
						formBean.setVerificadosSisben(verificados);
						request.setAttribute("verificadosSisben", verificados);
					}
				} else if (tipo.equalsIgnoreCase(BENEFICIARIO)) {
					verificados = (ArrayList<VerificacionVO>) verificacionServiceImpl
							.findVerificationBeneficiario(user.getName());
					if (!verificados.isEmpty()) {
						formBean.setVerificadosBen(verificados);
						request.setAttribute("verificadosBen", verificados);
					}
				}
			}

		} catch (DAOException e) {
			SUBException.logDebugError("", e);
			formBean.setMsg("Ocurrio un error en la generacion del reporte.");
			return mapping.findForward("success");
		}

		return mapping.findForward("success");
	}
}
